package top.wardyan.wdev.dbassist.action.base;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import top.wardyan.wdev.dbassist.action.table.TableShell;
import top.wardyan.wdev.dbassist.config.DDA;

/**
 * 只有一个子类*_col的
 * @author ward
 */
public class BaseColShell extends BaseShell<BaseColShell>{

	protected Composite mainCom;
	protected Composite colCom;
	protected Table table;
	protected Table colTable;
	
	
	private void resize() {
		mainCom.setBounds(0, topCom.getLocation().y + topCom.getSize().y + 2, self.getSize().x / 2, self.getSize().y - topCom.getLocation().y - topCom.getSize().y - 2);
		table.setBounds(0, 0, mainCom.getSize().x, mainCom.getSize().y - 30);
		
		//colTable.setBounds(700, 100, 1000, 300);
		colCom.setBounds(mainCom.getLocation().x + mainCom.getSize().x + 5, mainCom.getLocation().y, this.getSize().x - mainCom.getSize().x - mainCom.getLocation().x - 10, mainCom.getSize().y);
		colTable.setBounds(0, 0, colCom.getSize().x, colCom.getSize().y - 30);
	}
	public BaseColShell() {
		super();
		//setSize(1024, 720);
		createContents();
		addControlListener(new ControlAdapter() {
			@Override
			public void controlResized(ControlEvent e) {
				resize();
			}
		});
		this.resize();
	}
	
	
	
	private void createContents() {
		mainCom = new Composite(this, SWT.BORDER);
		mainCom.setLocation(0, topCom.getLocation().y + topCom.getSize().y + 2);
		
		table = new Table(mainCom, SWT.NONE);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		table.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int index = table.getSelectionIndex();
				if(index >= 0) {
					TableItem item = table.getItem(index);
					String objectID = item.getText(0);
					//System.out.println(objectID);
					self.show(Integer.valueOf(objectID));
				}
				//table.getItem()
			}
		});
		
		TableColumn col = new TableColumn(table, SWT.NONE);
		col.setWidth(0);
		col.setAlignment(SWT.LEFT);
		col.setText("object_id");
		
		col = new TableColumn(table, SWT.NONE);
		col.setWidth(200);
		col.setAlignment(SWT.LEFT);
		col.setText("名称");
		
		col = new TableColumn(table, SWT.NONE);
		col.setWidth(200);
		col.setAlignment(SWT.LEFT);
		col.setText("描述");
		
		col = new TableColumn(table, SWT.NONE);
		col.setWidth(100);
		col.setAlignment(SWT.LEFT);
		col.setText("备注");
		
		col = new TableColumn(table, SWT.NONE);
		col.setWidth(60);
		col.setAlignment(SWT.LEFT);
		col.setText("状态");
		
		col = new TableColumn(table, SWT.NONE);
		col.setWidth(60);
		col.setAlignment(SWT.LEFT);
		col.setText("所有者");
		
		col = new TableColumn(table, SWT.NONE);
		col.setWidth(60);
		col.setAlignment(SWT.LEFT);
		col.setText("AUTHOR");
		
		col = new TableColumn(table, SWT.NONE);
		col.setWidth(60);
		col.setAlignment(SWT.LEFT);
		col.setText("MENDER");
		
		col = new TableColumn(table, SWT.NONE);
		col.setWidth(60);
		col.setAlignment(SWT.LEFT);
		col.setText("CHECKER");
		
		col = new TableColumn(table, SWT.NONE);
		col.setWidth(60);
		col.setAlignment(SWT.LEFT);
		col.setText("AUDITOR");
		
		col = new TableColumn(table, SWT.NONE);
		col.setWidth(120);
		col.setAlignment(SWT.LEFT);
		col.setText("最后更新");
		
		col = new TableColumn(table, SWT.NONE);
		col.setWidth(50);
		col.setAlignment(SWT.LEFT);
		col.setText("版本");
		
		List<Record> rs = this.list();
		//System.out.println(rs.size());
		for(Record r : rs) {
			TableItem item = new TableItem(table, SWT.NONE);
			item.setText(new String[] {
					r.getStr("object_id"), 
					r.getStr("name"), 
					r.getStr("xDesc"), 
					r.getStr("xNote"), 
					r.getStr("sStatus"), 
					r.getStr("rOwner_ID"), 
					r.getStr("rAuthor_ID"), 
					r.getStr("rMender_ID"), 
					r.getStr("rCheck_ID"), 
					r.getStr("rAuditor_ID"), 
					r.getStr("sLastUpdate"), 
					r.getStr("sVersion")
			});
		}
		//table.pack();
		/**
		rAuthor_ID		INT NOT NULL,				-- 创建者
		rOwner_ID		INT NOT NULL,				-- 当前拥有者
		rMender_ID		INT NOT NULL,				-- 提交
		rChecker_ID		INT NOT NULL,				-- 复核
		rAuditor_ID		INT NOT NULL,				-- 审核
		sCreateTime		DATETIME NOT NULL,
		sLastUpdate		DATETIME NOT NULL,
		sStatus			TINYINT NOT NULL,			-- 状态 1废弃(锁定), 2编辑, 3提交, 4复核, 5审核(锁定), 6锁定. 锁定时不允许其他人修改,只允许拥有者修改
		sVersion		INT NOT NULL,
		 */
		
		
		
		//TableItem item = new TableItem(table, SWT.NONE);
		colCom = new Composite(this, SWT.NONE);
		colCom.setBounds(mainCom.getLocation().x + mainCom.getSize().x + 5, mainCom.getLocation().y, this.getSize().x - mainCom.getSize().x - mainCom.getLocation().x - 5, mainCom.getSize().y);
		
		colTable = new Table(colCom, SWT.NONE);
		colTable.setHeaderVisible(true);
		colTable.setLinesVisible(true);
		
		col = new TableColumn(colTable, SWT.NONE);
		col.setWidth(100);
		col.setAlignment(SWT.LEFT);
		col.setText("名称");
		
		col = new TableColumn(colTable, SWT.NONE);
		col.setWidth(100);
		col.setAlignment(SWT.LEFT);
		col.setText("数据类型");
		
		col = new TableColumn(colTable, SWT.NONE);
		col.setWidth(100);
		col.setAlignment(SWT.LEFT);
		col.setText("数据字典");
		
		col = new TableColumn(colTable, SWT.NONE);
		col.setWidth(100);
		col.setAlignment(SWT.LEFT);
		col.setText("说明");
		
		col = new TableColumn(colTable, SWT.NONE);
		col.setWidth(100);
		col.setAlignment(SWT.LEFT);
		col.setText("提示");
		
		col = new TableColumn(colTable, SWT.NONE);
		col.setWidth(50);
		col.setAlignment(SWT.LEFT);
		col.setText("备注");
		
		col = new TableColumn(colTable, SWT.NONE);
		col.setWidth(50);
		col.setAlignment(SWT.LEFT);
		col.setText("状态");
		
		col = new TableColumn(colTable, SWT.NONE);
		col.setWidth(50);
		col.setAlignment(SWT.LEFT);
		col.setText("rAuthor_ID");
		
		col = new TableColumn(colTable, SWT.NONE);
		col.setWidth(50);
		col.setAlignment(SWT.LEFT);
		col.setText("最后编辑");
		
		col = new TableColumn(colTable, SWT.NONE);
		col.setWidth(50);
		col.setAlignment(SWT.LEFT);
		col.setText("版本");
	}
	
	private void show(Integer objectID) {
		List<Record> rs = Db.find("select m.*, t.name as typename, n.* from sys.columns as m inner join sys.tables as b on b.object_id = m.object_id left join sys.types as t on t.system_type_id = m.system_type_id left join dbo.yt_table_col as n on n.xMainName = b.name where m.object_id = ?", objectID);
		//System.out.println(rs.size());
		colTable.removeAll();
		if(rs != null && rs.size() > 0) {
			for(Record r : rs) {
				//System.out.println(r.getStr("name"));
				TableItem item = new TableItem(colTable, SWT.NONE);
				item.setText(new String[] {
						r.getStr("name"),
						r.getStr("typename"),
						r.getStr("rDic_ID"),
						r.getStr("xDesc"),
						r.getStr("xHint"),
						r.getStr("xNote"),
						r.getStr("sStatus"),
						r.getStr("rAuthor_ID"),
						r.getStr("sLastUpdate"),
						r.getStr("sVersion")
						
				});
			}
		}
	}
	
	private List<Record> list(){
		DDA.getInstance();
		return Db.find("select m.object_id, m.name, n.* from sys.tables as m left join dbo.yt_table as n on m.name = n.xName order by name ASC");
	}
	
	public static void main(String[] args) {
		new TableShell().go();
	}

	@Override
	protected String title() {
		return "数据表";
	}

}
